package cn.edu.xust.service.impl;


import cn.edu.xust.configuration.exception.Default200Exception;
import cn.edu.xust.configuration.exception.ErrorConstant;
import cn.edu.xust.dao.SysRoleprivilegeDao;
import cn.edu.xust.entity.SysMenuprivilegeEntity;
import cn.edu.xust.entity.SysRoleprivilegeEntity;
import cn.edu.xust.entity.SysUserEntity;
import cn.edu.xust.entity.SysUserroleEntity;
import cn.edu.xust.service.SysMenuprivilegeService;
import cn.edu.xust.service.SysRoleprivilegeService;
import cn.edu.xust.service.SysUserService;
import cn.edu.xust.service.SysUserroleService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("sysRoleprivilegeService")
public class SysRoleprivilegeServiceImpl extends ServiceImpl<SysRoleprivilegeDao, SysRoleprivilegeEntity> implements SysRoleprivilegeService {



    @Autowired
    @Lazy
    private SysUserService sysUserService;

    @Autowired
    @Lazy
    private SysUserroleService sysUserroleService;
    @Autowired
    @Lazy
    private SysMenuprivilegeService sysMenuprivilegeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleprivilegeEntity> page = this.page(
                new Query<SysRoleprivilegeEntity>().getPage(params),
                new QueryWrapper<SysRoleprivilegeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Integer> getByrolesDistinct(List<Integer> roleids) {

        QueryWrapper<SysRoleprivilegeEntity> wap = new QueryWrapper<SysRoleprivilegeEntity>().in("roleid", roleids);
        List<SysRoleprivilegeEntity> sysRoleprivilegeEntities = baseMapper.selectList(wap);
        List<Integer> pids = sysRoleprivilegeEntities.stream().map(SysRoleprivilegeEntity::getPrivelegeid).collect(Collectors.toList());

        //去重
        HashSet<Integer> set = new HashSet<>(pids);
        ArrayList<Integer> integers = new ArrayList<>(set);

        return integers;


    }

    @Override
    public List<SysRoleprivilegeEntity> getByroles(List<Integer> roleids) {

        QueryWrapper<SysRoleprivilegeEntity> wap = new QueryWrapper<SysRoleprivilegeEntity>().in("roleid", roleids);
        return baseMapper.selectList(wap);


    }

    @Override
    public List<SysRoleprivilegeEntity> getByroles(String roleid) {

        QueryWrapper<SysRoleprivilegeEntity> wap = new QueryWrapper<SysRoleprivilegeEntity>().eq("roleid", roleid);
        return baseMapper.selectList(wap);

    }

    @Override
    public int deleteByRoleid(Integer roleid) {
        QueryWrapper<SysRoleprivilegeEntity> wap = new QueryWrapper<>();
        wap.eq("roleid", roleid);
        return baseMapper.delete(wap);
    }

    @Override
    public List<Integer> getPrevByUsername(String username) throws Default200Exception {

        SysUserEntity byName = sysUserService.getByName(username);
        if (byName == null) {
            throw new Default200Exception(ErrorConstant.user_not_exist);
        }

        //获取角色
        List<SysUserroleEntity> byUid = sysUserroleService.getByUid(byName.getUid());
        List<Integer> roles = byUid.stream().map(SysUserroleEntity::getRoleid).collect(Collectors.toList());


        //如果是开发者 1是开发者 就去获取所有权限
        if(roles.contains(1)){
            return sysMenuprivilegeService.list().stream().map(SysMenuprivilegeEntity::getPrivilegeid).collect(Collectors.toList());
        }

        //不是开发者，就只获取角色权限
        List<SysRoleprivilegeEntity> byroles = this.getByroles(roles);
        List<Integer> collect = byroles.stream().map(SysRoleprivilegeEntity::getPrivelegeid).collect(Collectors.toList());

        //去重
       return collect.stream().distinct().collect(Collectors.toList());


    }

}